home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10189 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  82 lines

  1. Path: dekalb.dc.peachnet.edu!not-for-mail
  2. From: williamh@dekalb.dc.peachnet.edu (darlene williams)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can copy constructor and operator= share code?
  5. Date: 5 Mar 1996 19:56:48 -0500
  6. Organization: DeKalb College
  7. Sender: gregory ulyssis narizny
  8. Message-ID: <4hinsg$ooq@dekalb.dc.peachnet.edu>
  9. References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred>
  10. NNTP-Posting-Host: dekalb.dc.peachnet.edu
  11.  
  12. i noticed that there is a lot of confusion lately in this group about the
  13. difference between assignment operator, default, and copy constructors:
  14.  
  15. >(Boris Burtin) wrote:
  16. >>      I have noticed that a copy constructor and operator= perform pretty
  17. >>      much the same function..
  18.  
  19.     yes, but the context of the operation is different:
  20.         copy c-tor: newly created instance
  21.         = op.:      copies after the instance has been instantiated
  22.     the main difference in terms of your code is that any post-construction
  23.     initialization may have to be undone, i.e., de-allocation of resources
  24.     done at construction.
  25.  
  26. >> Is there a way for the one of the two functions to call the other, to
  27. >> avoid duplicate code?
  28.  
  29.     i have adopted using a "copy()" member function; the benefit is a single
  30.     piece of code, and the cost is the probable double-initialization of
  31.     class members:
  32.  
  33.         class foo
  34.         {
  35.         public:
  36.             foo () { }
  37.             foo &operator = (const foo &rhs) { copy (rhs); return (*this); }
  38.             foo (const foo &rhs) { copy (rhs); }
  39.             void copy (const foo &rhs) { b = rhs.b; }
  40.         private:
  41.             bar b;
  42.         };
  43.  
  44.     the cost referred to above is that 'bar b' will get initialized twice
  45.     in the copy constructor case, once by its default c-tor, and again by
  46.     its assignment operator ("b=rhs.b"), as opposed to:
  47.  
  48.             void foo::foo (const foo &rhs) b(rhs.b) { }
  49.  
  50.     as to this approach:
  51.  
  52. >T &T::operator =(const T &t) { ~T(); new(this) T(t); }
  53.  
  54.     this is a somewhat interesting approach, but destruction isn't
  55.     always the same as "resetting" the object to a pre-copy state.
  56.     i prefer including a "reset()" function whose semantics imply
  57.     'reset to at-construction state', and not have to worry about
  58.     any side-effects of destroying the object:
  59.         T &T::operator = (const T &t) { reset(); copy(t); return(*this); }
  60.  
  61.  
  62. on another note: i haven't visited this group (or the net) in a while..
  63. are there any groups like "comp.lang.c++.wizards"?; i'm looking for
  64. info beyond c++ mechanics:
  65.  
  66.         -- has anyone had experiences/comment re. coplien's exemplars
  67.                 (class community bulletin board) technique?
  68.         -- a year ago, i saw a reference to a book that went into
  69.                 c++ mixins.. where can i find more mixin info?
  70.                 (john max skaller.. you still out there??)
  71.         -- what's the latest on RTTI?
  72.  
  73.  
  74. gregory narizny
  75.  
  76.         greg@bossy.bst.bls.com
  77.  
  78.         -- currently doing multi-point video conference scheduling systems
  79.             (in the middle of the phone deregulation wars!)
  80.         -- commercial inquiries for s/w development work always welcome
  81.  
  82.